package com.czx.recyclerview;

import android.content.pm.PackageManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import java.util.List;

public class GridRVAdapter extends RecyclerView.Adapter {

    public static final int VIEW_TYPE_HEADER = 0;
    public static final int VIEW_TYPE_BODY = 1;

    private List<String> mList;

    public GridRVAdapter(List<String> list) {
        mList = list;
    }

    public boolean isHeader(int position) {
        return position == 0;
    }

    @Override
    public int getItemViewType(int position) {
        return isHeader(position) ? VIEW_TYPE_HEADER : VIEW_TYPE_BODY;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == VIEW_TYPE_HEADER) {
            return new HeaderViewHolder(LayoutInflater.from(parent.getContext()).inflate(
                    R.layout.gridview_list_item, parent, false));
        } else {
            //can use different layout,in this sample just need a TextView so i use same layout
            return new BodyViewHolder(LayoutInflater.from(parent.getContext()).inflate(
                    R.layout.gridview_list_item, parent, false));
        }

    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (isHeader(position)) {
            ((HeaderViewHolder) holder).getTextView().setText("This is the header!");
        } else {
            ((BodyViewHolder) holder).getTextView().setText(mList.get(position-1));
        }
    }

    @Override
    public int getItemCount() {
        return mList.size() + 1;
    }

    private class HeaderViewHolder extends RecyclerView.ViewHolder {

        private TextView textView;

        HeaderViewHolder(View itemView) {
            super(itemView);
            textView = (TextView) itemView.findViewById(R.id.text);
        }

        public TextView getTextView() {
            return textView;
        }
    }

    private class BodyViewHolder extends RecyclerView.ViewHolder {

        private TextView textView;

        BodyViewHolder(View itemView) {
            super(itemView);
            textView = (TextView) itemView.findViewById(R.id.text);
        }

        public TextView getTextView() {
            return textView;
        }
    }
}
